import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors, Query } from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { Role } from './entities/role.entity';
import { BaseController } from 'src/common/BaseController';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { ResponsInterceptoreBase } from '../../../transform.interceptor';
import { QueryRoleDto } from './dto/query-role.dto';
import { UpdateRoleMenusDto } from './dto/role-menu.dto';
import { CurrentUser, Public } from 'src/app.decorator';
@ApiTags('角色管理')

@Controller('role')
@UseInterceptors(ResponsInterceptoreBase)
export class RoleController extends BaseController<Role,RoleService> {
  constructor(private readonly roleService: RoleService) {
    super(roleService); // Pass the service to the base controller
  }

  @Public()
  @Post("addRole")
  @ApiOperation({ summary: '创建角色' })
  async create(@Body() createRoleDto: CreateRoleDto, @CurrentUser() currUser: any) {
    this.InitEntity(createRoleDto, currUser);
    let result = await this.roleService.create(createRoleDto);
    console.log(result);
    if(result[0]) {
      return this.Success(result[1], "角色添加成功");
    }else{
      return this.Fail(result[1], "角色添加失败");
    }
  }

  @Post('updateRole')
  @ApiOperation({ summary: '更新角色' })
  async update(@Body() updateRoleDto: UpdateRoleDto, @CurrentUser() currUser: any) {
    this.updateEntity(updateRoleDto, currUser);
    let result = await this.roleService.update(updateRoleDto);
    if(result[0]) {
      return this.Success(result[1], "角色更新成功");
    }else{
      return this.Fail(result[1], "角色更新失败");
    }
  }

  @Post('DeleteRole')
  @ApiOperation({ summary: '删除角色' })
  async remove(@Body() IdArr: Array<string>) {
    let result = await this.roleService.remove(IdArr);
    if(result[0]) {
      return this.Success(result[1], "角色删除成功");
    }else{
      return this.Fail(result[1], "角色删除失败");
    }
  }

  @Get('list')
  @ApiOperation({ summary: '获取角色列表' })
  async GetRoleList(@Query() query: QueryRoleDto, @CurrentUser() currUser: any){
    console.log(query, currUser);
    let roles = await this.roleService.GetRoleList(query, currUser.tenantId);
    return this.Success(roles, "角色列表获取成功");
  }

  @Post('UpdateRoleMenus')
  @ApiOperation({ summary: '更新角色菜单列表' })
  async UpdateRoleMenus(@Body() params: UpdateRoleMenusDto){
    let roles = await this.roleService.UpdateRoleMenus(params);
    return this.Success(roles, "角色列表更新成功");
  }

  @Get("getMenusByRoleId")
  @ApiOperation({ summary: '获取角色菜单' })
  async getMenusByRoleId(@Query('Id') Id: string){
    let data =await this.roleService.getMenusByRoleId(Id);
    console.log(data);
    return this.Success(data, "查询成功！");
  }

  @Get("getFlatMenusByRoleId")
  @ApiOperation({ summary: '获取角色菜单-平铺数组' })
  async getFlatMenusByRoleId(@Query('Id') Id: string){
    let data =await this.roleService.getFlatMenusByRoleId(Id);
    console.log(data);
    return this.Success(data, "查询成功！");
  }


 
}
